/***************************************************************************
 * 文件名：CircleButtoncs
 * 功能：
 * 说明： 圆形按钮
 * -------------------------------------------------------------------------
 * 创建时间：2025/7/9 14:03:36
 * 创建人：葛浩
 * 邮箱： 2094733071@qq.com
 * =========================================================================
 *
 * 修改人：   
 * 修改时间：   
 * 修改说明：   
 ***************************************************************************/
using DevExpress.XtraEditors;
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace AuxiliaryTools
{
	public class CircleButtoncs:SimpleButton
	{
		/// <summary>
		/// 重绘事件
		/// </summary>
		/// <param name="e"></param>
		protected override void OnPaint(PaintEventArgs e)
		{
			SimpleButton btn = this;
			if(btn == null) { return; }
			Rectangle rect = btn.ClientRectangle;

			// 确保圆形比例（强制为正方形）
			int diameter = Math.Min(rect.Width, rect.Height);
			Rectangle circleRect = new Rectangle(
				(rect.Width - diameter) / 2,
				(rect.Height - diameter) / 2,
				diameter,
				diameter
			);

			// 1. 绘制圆形背景
			Color bgColor = Color.FromArgb(195, 173, 227);
			using (Brush bgBrush = new SolidBrush(bgColor))
			{
				e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
				e.Graphics.FillEllipse(bgBrush, circleRect);
			}

			// 2. 绘制边框（可选）
			using (Pen borderPen = new Pen(Color.SteelBlue, 1.5f))
			{
				e.Graphics.DrawEllipse(borderPen, circleRect);
			}

			// 3. 绘制图标（如果有）
			if (btn.Image != null)
			{
				Image img = btn.Image;
				Point imgPos = new Point(
					circleRect.X + (circleRect.Width - img.Width) / 2,
					circleRect.Y + (circleRect.Height - img.Height) / 2
				);
				e.Graphics.DrawImage(img, imgPos);
			}

			// 4. 绘制文本（居中）
			if (!string.IsNullOrEmpty(btn.Text))
			{
				TextFormatFlags flags = TextFormatFlags.HorizontalCenter |
									  TextFormatFlags.VerticalCenter |
									  TextFormatFlags.WordBreak;

				TextRenderer.DrawText(
					e.Graphics,
					btn.Text,
					btn.Font,
					circleRect,
					btn.Enabled ? btn.Appearance.ForeColor : Color.White,
					flags
				);
			}
		}
	}
}
